   
$(function(){
     
    if(globalStoreId=='' || globalStoreId==undefined){
 	  layui.use([ 'layer' ], function(){
		 
		 Layer.showFailMsg("请先选择具体门店！");
		 document.getElementById("addPeople").style.visibility="hidden"; 
	 })
  }else{
	     loadBaseInfo(globalStoreId);
	     getUserId(globalStoreId);
 	   
 	    layui.use(['table','layer','form','laydate'], function(){
	        var $ = layui.$
	            ,layer = layui.layer
	            ,form = layui.form
	            ,table = layui.table
	            ,laydate = layui.laydate;
	        //展示已知数据
	        table.render({
	            elem: '#idTest'
	            ,url: hostUrl+'/dealerPeople/dealerPeople?dealerId='+globalDealerId+"&storeId="+globalStoreId
	            ,cols: [[ //标题栏
	                {field: 'position', title: '职位'}
	                ,{field: 'name', title: '姓名'}
	                ,{field: 'sex', title: '性别', minWidth: 120}
	                ,{field: 'phone', title: '电话'}
	                ,{field: 'age', title: '年龄'}
	                ,{field: 'education', title: '学历'}
	                ,{field: 'major', title: '专业'}
	                ,{field: 'work_year', title: '工龄',minWidth: 160}
	                ,{field: 'entry_data', title: '入职日期',minWidth: 160}
	                ,{field: 'tool',title: '操作',fixed: 'right', minWidth:188, align:'center', toolbar: '#barDemo'}
	            ]]
	            ,cellMinWidth: 120
	           
	            ,page: true //是否显示分页
	        });

	        //监听新增按钮

	        //监听表格复选框选择
	        table.on('checkbox(listTable)', function(obj){
	            console.log(obj)
	        });
	        
	        //监听工具条
	        table.on('tool(listTable)', function(obj){
	            var data = obj.data;
	            if(obj.event === 'details'){
	            	dataDetail=data;
	            	var detail=layer.open({
	                    type: 2
	                    ,id: (new Date()).valueOf()
	                    ,title: '编辑人员档案'
	                    ,content: hostUrl+'/store/peopleDetail.do'
	                    ,area:  ['500px', '500px']
	                    ,maxmin: true
	                    ,end: function(){
	                    	dataDetail=null;
	                    }
	                }); 
	            	layer.full(detail);
	            } else if(obj.event === 'edit'){
	            	dataDetail=data;
	            	var detail=layer.open({
	                    type: 2
	                    ,id: (new Date()).valueOf()
	                    ,title: '编辑人员档案'
	                    ,content: hostUrl+'/store/peopleEdit.do'
	                    ,area:  ['500px', '500px']
	                    ,maxmin: true
	                    ,end: function(){
	                    	dataDetail=null;
	                    }
	                }); 
	            	layer.full(detail);
	            }
	            else if(obj.event === 'soldOutShlef'){
	            	 var outUserId=data.user_id;
	            	$("#alert_select").show();
	                $("#alert_select .layui-layer-btn0").click(function () {
	                	var id= $("#alert_select").find("select").val();
	               	  if(id=='' || id==undefined){
	               		 Layer.showFailMsg("账号不能为空");
	               		 return;
	               	  }
	                    layer.confirm('确定迁移吗?', function(index){
 	                        var intoUserId=$("#alert_select").find("select").val();
	                        //userIdMove(intoUserId,outUserId);
 	                       $.ajax({
 	              	        url:  hostUrl + "/dealerPeople/userIdMove",
 	              	        type: 'post',//提交的方式
 	              	        dataType:'json',
 	              	        async:false,
 	              	        data: {"intoUserId":intoUserId,"outUserId":outUserId},
 	              	        success: function(obj) {
 	               	        	if( obj.status==0){
 	               	        		Layer.showFailMsg("迁移成功");
 	               	        		 $("#alert_select").hide();
 	                                   layer.close(index);
 	              	        	} else {
 	              	       		 layer.msg(obj.msg);
 	              	        	}  
 	              	        },
 	              	        error:function(result) {
 	              				Layer.showFailMsg("操作失败");
 	              	        }
 	              		})
	                        
	                    });
	                })
	               $("#alert_select .layui-layer-btn2").click(function () {
	                   $("#alert_select").hide();
	               })
	            }
	        });

	        var $ = layui.$, active = {
	            getCheckData: function(){ //获取选中数据
	                var checkStatus = table.checkStatus('idTest')
	                    ,data = checkStatus.data;
	                layer.alert(JSON.stringify(data));
	            }
	            ,getCheckLength: function(){ //获取选中数目
	                var checkStatus = table.checkStatus('idTest')
	                    ,data = checkStatus.data;
	                layer.msg('选中了：'+ data.length + ' 个');
	            }
	            ,isAll: function(){ //验证是否全选
	                var checkStatus = table.checkStatus('idTest');
	                layer.msg(checkStatus.isAll ? '全选': '未全选')
	            }
	        };

	        $('.layadmin-takerates .layui-btn').on('click', function(){
	            var type = $(this).data('type');
	            active[type] ? active[type].call(this) : '';
	        });
	         
	        //执行一个laydate实例
	        laydate.render({
	            elem: '#date'
	        });

	        form.render();

	        
	        
	    });
  }
 $("#addPeople").click(function () {
		peopleAdd();
	})
	  function peopleAdd() {
	            var index = layer.open({
	                type: 2
	                ,id: (new Date()).valueOf()
	                ,title: '新建经销商'
	                //,content: 'dealerAdd.do'
	                ,content: hostUrl+'/store/peopleAdd.do'
	                ,area: ['500px', '500px']
//	                ,btn: ['保存', '取消']
	                ,maxmin: true
	                ,yes: function (layero,index) {
	                	 
	                }
	                ,btn2: function (index, layero) {
	                	 layer.close(index);
	                }
	            });
	            layer.full(index);
	        }
 
	 
	 
  
  
}); 

function loadBaseInfo(globalStoreId){
	 var globalStoreId=globalStoreId;
 	 $.ajax({
	        url:  hostUrl + "/dealer/storeDetail",
	        type: 'post',//提交的方式
	        dataType:'json',
	        async:false,
	        data: {"storeId":globalStoreId},
	        success: function(obj) {
	        	if( obj.status==1){
 	        		var resultDealer = obj.dealer;
	        		var resultStore = obj.store[0];
	        		var addressDealer=resultDealer.stock_province+resultDealer.stock_city+resultDealer.stock_town+resultDealer.stock_address;
	        		var addressStore=resultStore.store_province+resultStore.store_city+resultStore.store_town+resultStore.store_address;

 	        		//经销商信息 
	               	$("#dealerName").val(resultDealer.dealer_name);
	        		$("#authorizationLimit").val(resultDealer.authorization_limit);  
	        		$("#dealerPrivince").val(resultDealer.stock_province);
	        		$("#dealerCity").val(resultDealer.stock_city);
	        		$("#dealerTown").val(resultDealer.stock_town);
	        		$("#dealerAddress").val(addressDealer);
	        	    $("#legalpersonName").val(resultDealer.legalperson_name);
	        		$("#legalpersonSex").val(resultDealer.legalperson_sex);
	        		$("#legalpersonPhone").val(resultDealer.legalperson_phone);
	         		$("#actualpersonName").val(resultDealer.actualperson_name);
	         		$("#actualpersonSex").val(resultDealer.actualperson_sex);
	         		$("#actualpersonPhone").val(resultDealer.actualperson_phone);
	          		//门店信息  
	         		 $("#storeName").val(resultStore.store_name);
	         		$("#storeCode").val(resultStore.store_code);
	         		$("#storeProvince").val(resultStore.store_province);
	         		$("#storeCity").val(resultStore.store_city);
	         		$("#storeTown").val(resultStore.store_town);
	         		$("#storeAddress").val(addressStore); 
	         		var busTypes='';
	         		for(var i=0;i<obj.store.length;i++){
	         			  busTypes+=obj.store[i].open_business;
	         			  busTypes+=",";
	         		}
	         		$("#busType").val(busTypes); 
	         		
	        	} else {
	       		 layer.msg(obj.msg);
	       	}  
	        },
	        error:function(result) {
				Layer.showFailMsg("操作失败");
	        }
		})
 
 }   
//查询账号
 function getUserId(storeId){
	 var storeId=storeId;
   	 $.ajax({
 	        url:  hostUrl + "/dealerPeople/queryuserIdByStoreId",
 	        type: 'post',//提交的方式
 	        dataType:'json',
 	        async:false,
 	        data: {"storeId":storeId},
 	        success: function(obj) {
  	        	if( obj.status==0){
 	        		 var result = obj.data;
    	        		  
 	        		 $("#userId").append('<option value="">请选择账号</option>');
	        		 for(var i=0;i<result.length;i++){
	 				  $("#userId").append('<option value='+result[i].user_id+'>'+ result[i].user_id+'</option>');
	                  }
  	        	} else {
 	       		 layer.msg(obj.msg);
 	        	}  
 	        },
 	        error:function(result) {
 				Layer.showFailMsg("操作失败");
 	        }
 		})
   }  
 
    